package com.lb.jwitter.action.report;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXAHocVienTamDungHocKy;
import com.lb.jwitter.service.TblQLDTTXAHocVienTamDungHocKyService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienTamDungHocKyReportAction{
    private Message msg; 
    public Message getMsg() { return msg; }

    private Long hocKyId;
    
   public Long getHocKyId() {
		return hocKyId;
	}
	public void setHocKyId(Long hocKyId) {
		this.hocKyId = hocKyId;
	}
   
    private List<TblQLDTTXAHocVienTamDungHocKy> tblQLDTTXAHocVienTamDungHocKys;
	
    public List<TblQLDTTXAHocVienTamDungHocKy> getTblQLDTTXAHocVienTamDungHocKys() {
		return tblQLDTTXAHocVienTamDungHocKys;
	}
    
    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams;

	public List<TblQLDTTXAHocKyTrongNam> getTblQLDTTXAHocKyTrongNams() {
		return tblQLDTTXAHocKyTrongNams;
	}

	private String report;
    
    private TblQLDTTXAHocVienTamDungHocKyService tblQLDTTXAHocVienTamDungHocKyService;
    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService;

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_TamDung_HocKy", "xem")){ 
        return "noprivilege"; 
    } 
 
    tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 
        


        msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>();
        
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("id");
            con.setValue1("desc");
            con.setOperator("order");
            cons.add(con);	
        } 
        
       tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);
        
        
        
        return Action.SUCCESS;
	 }catch(Exception e){ 
	     System.out.println("Loi Report tblQLDTTXAHocVienTamDungHocKy Action"); 
	     e.printStackTrace(); 
	     return Action.ERROR; 
	}
}
 
 
 
 
 public String execute() throws Exception 
 { 
  try{ 	
      Map session = ActionContext.getContext().getSession();
      taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
      if( taiKhoanDangNhap == null) return "noprivilege";
     ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_TamDung_HocKy", "xem")){ 
         return "noprivilege"; 
     } 
     	tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient");
     	tblQLDTTXAHocVienTamDungHocKyService = (TblQLDTTXAHocVienTamDungHocKyService)  context.getBean("tbl_QLDTTXA_HocVien_TamDung_HocKyClient");
         
         msg = new Message(); 
         List<ConditionReport> cons = new ArrayList<ConditionReport>();
         
         TblQLDTTXAHocKyTrongNam hocKy = null;
         TblQLDTTXAHocKyTrongNam hocKyTruoc = null;
         
         if(hocKyId != null && !hocKyId.equals(new Long(0))){
        	 hocKy = tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(hocKyId);        	 
        	 hocKyTruoc = tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(hocKyId - 1);
         }
         
         List<TblQLDTTXAHocVienTamDungHocKy> hocVienTamDungHocKys;
         List<TblQLDTTXAHocVienTamDungHocKy> hocVienTamDungHocKyTruocs;
         tblQLDTTXAHocVienTamDungHocKys = new ArrayList<TblQLDTTXAHocVienTamDungHocKy>();
         
         if(hocKy != null){
        	 cons.clear();
             { 
                 ConditionReport con = new ConditionReport(); 
                 con.setCol("hocKyId.id");
                 con.setValue1(hocKy.getId());
                 con.setOperator("eq");
                 cons.add(con);	
             } 
             
             { 
                 ConditionReport con = new ConditionReport(); 
                 con.setCol("hocVienId.ten");
                 con.setValue1("asc");
                 con.setOperator("order");
                 cons.add(con);	
             } 
             { 
                 ConditionReport con = new ConditionReport(); 
                 con.setCol("hocVienId.hoTen");
                 con.setValue1("asc");
                 con.setOperator("order");
                 cons.add(con);	
             } 
             hocVienTamDungHocKys = tblQLDTTXAHocVienTamDungHocKyService.findtblQLDTTXAHocVienTamDungHocKyByCondition(cons, 0, 0);
         } else {
        	 hocVienTamDungHocKys = new ArrayList<TblQLDTTXAHocVienTamDungHocKy>();
         }
         
         if(hocKyTruoc != null){
        	 cons.clear();
             { 
                 ConditionReport con = new ConditionReport(); 
                 con.setCol("hocKyId.id");
                 con.setValue1(hocKyTruoc.getId());
                 con.setOperator("eq");
                 cons.add(con);	
             } 
             
             { 
                 ConditionReport con = new ConditionReport(); 
                 con.setCol("hocVienId.ten");
                 con.setValue1("asc");
                 con.setOperator("order");
                 cons.add(con);	
             } 
             { 
                 ConditionReport con = new ConditionReport(); 
                 con.setCol("hocVienId.hoTen");
                 con.setValue1("asc");
                 con.setOperator("order");
                 cons.add(con);	
             } 
             hocVienTamDungHocKyTruocs = tblQLDTTXAHocVienTamDungHocKyService.findtblQLDTTXAHocVienTamDungHocKyByCondition(cons, 0, 0);
         } else {
        	 hocVienTamDungHocKyTruocs = new ArrayList<TblQLDTTXAHocVienTamDungHocKy>();
         }
         
         for(TblQLDTTXAHocVienTamDungHocKy hvTamDungHocKy : hocVienTamDungHocKys){
        	 boolean hit = false;
        	 for(TblQLDTTXAHocVienTamDungHocKy hvTamDungHocKyTruoc : hocVienTamDungHocKyTruocs){
        		 if(hvTamDungHocKyTruoc.getHocVienId().getId().equals(hvTamDungHocKy.getHocVienId().getId())){
        			 hit = true;
        			 break;
        		 }        			 
        	 }
        	 if(hit)
        		 tblQLDTTXAHocVienTamDungHocKys.add(hvTamDungHocKy);
         }
          
		  report ="<tbody>";
		  int stt = 0;
		  //DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		  
		  for(TblQLDTTXAHocVienTamDungHocKy hocVien : tblQLDTTXAHocVienTamDungHocKys){ 
			   stt++;
		      report += "<tr class='tableitem' > "; 
		      //stt
		      report += "<TD>" +stt+ "</TD>"; 
		      //mshv
		      report += "<td>" + (hocVien.getHocVienId().getMSHV() == null ? "" : hocVien.getHocVienId().getMSHV()) + "</td> ";
		      //ho ten
		      report += "<td>" + (hocVien.getHocVienId().getHoTen() == null ? "" : hocVien.getHocVienId().getHoTen())+ "</td> ";
		      report += "<td>" + (hocVien.getHocVienId().getTen() == null ? "" : hocVien.getHocVienId().getTen())+ "</td> ";
		      
		     report += "<td>" + (hocVien.getHocVienId().getLopId() == null ? "" : hocVien.getHocVienId().getLopId().getTen()) + "</td> ";
		      report += "<td>" + (hocVien.getHocVienId().getKhoaId() == null ? "" : hocVien.getHocVienId().getKhoaId().getTen()) + "</td> ";
		      report += "<td>" + (hocVien.getHocVienId().getNganhId() == null ? "" : hocVien.getHocVienId().getNganhId().getTen()) + "</td> ";
		      
		  }

		  report += "</tbody>"; 
  
		  return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Report tblQLDTTXAHocVienTamDungHocKy Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 }

public String getReport() {
	return report;
}

public void setReport(String report) {
	this.report = report;
}

public void setMsg(Message msg) {
	this.msg = msg;
} 




 } 
 